String.prototype.getBytes = function() {
    var cArr = this.match(/[^\x00-\xff]/ig);
    return this.length + (cArr == null ? 0 : cArr.length);
}
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.lTrim = function() {
    return this.replace(/(^\s*)/g, "");
}
String.prototype.rTrim = function() {
    return this.replace(/(\s*$)/g, "");
}
String.prototype.toArray = function() {
    var arr = new Array(this.length);
    for (var i = 0; i < this.length; i++)arr[i] = this.charAt(i);
    return arr;
}
function hide(area) {
    area.hide();
    return disable(area);
}
function clean(area) {
    area.find("input,select,textarea").each(
            function() {
                cleanInput(this);
            });
}
function cleanInput(item) {
    var e = jQuery(item);
    var type = e.attr("type");
    if (e.hasClass("final")) {
        // todo nothing
    } else if ("radio" == type || "checkbox" == type) {
        e.attr("checked", "");
    } else if ("hidden" == type || "text" == type) {
        e.val("");
    }
    cleanError(e);
}
function disable(area, notClean) {
    area.find("input").each(
            function() {
                disableInput(this, notClean);
            });
}
function hideInput(area) {
    area.hide();
    return disableInput(area);
}
function showInput(area) {
    area.show();
    area.removeAttr("disabled");
}
function disableInput(item, notClean) {
    var e = jQuery(item);
    e.attr("disabled", "disabled");
    if (notClean) {
        return;
    }
    cleanInput(item)
}
function show(area) {
    area.show();
    area.find("input").removeAttr("disabled");
}

function cleanError(obj) {
    var e = jQuery(obj);
    e.removeClass("error");
    e.parent("td").find("label[class=error]").text("");
}
function cleanAllError(obj) {
    var e = jQuery(obj);
    e.find("input[class*=error]").removeClass("error");
    e.find("label[class=error]").text("");
}
function addError(obj, message) {
    var e = jQuery(obj);
    if (e.attr("class").indexOf("input_dict") < 0) {
        e.addClass("error");
    }
    var label = e.parent("td").find("label[class=error]");
    if (label.length) {
        label.html(message);
    } else {
        label = jQuery("<label/>").addClass("error").html(message || "");
        label.appendTo(e.parent("td"));
    }
    label.show();
}
function next() {
    var items = jQuery("input[type!=hidden][step!=false]");
    items.each(
            function(intIndex) {
                jQuery(this).bind("keydown", function() {
                    var e = arguments[0] || window.event;
                    var ck = e.charCode || e.keyCode;
                    if (ck == 13) {
                        if (jQuery(this).attr("class").indexOf("input_dict") > -1) {
                            var dic = jQuery("#DIC_CAPXOUS_" + jQuery(this).attr("id"));
                            if (dic.length > 0) {
                                if (dic.parent("div:visible").length > 0) {
                                    return;
                                }
                            }
                        }
                        var item = jQuery(this).attr("target");

                        if (item == undefined) {
                            items.filter(":eq(" + (intIndex + 1) + ")").select();
                        } else {
                            jQuery(item).select();
                        }
                    }
                });
            });
}
function isUsername(value) {
    return /^\w+$/.test(value);
}
function isEmpty(value) {
    return value == null || value.length == 0;
}
function isNumber(value) {
    return /^(?:\d{0,8})(?:\.\d{0,2})?$/.test(value);
    //return /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
}
function isWeight(value) {
    return /^(?:\d{0,7})(?:\.\d{0,3})?$/.test(value);
}
function isDigits(value) {
    return /^\d+$/.test(value);
}
function isTel(value) {
    return /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/.test(value);
}
function isFax(value) {
    return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/.test(value);
}
function isUrl(value) {
    return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/.test(value);
}
function isMail(value) {
    return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value);
}
function isDecimal(value, params) {

    return /^(?:\d{0,3})(?:\.\d{0,3})?$/.test(value);
}
function hasNumber(value) {
    for (var i = 0; i < value.length; i++) {
        var ch = value.charAt(i);
        if (ch >= '0' && ch <= '9')return true;
    }
    return false;
}
function byteMaxLength(value, length) {
    return value.getBytes() <= length;
}
function isDate(value) {
    var r = value.match(/^(\d{4})(-|\/)(\d{2})\2(\d{2})$/);
    if (r == null) return false;
    var d = new Date(r[1], r[3] - 1, r[4]);
    return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]);
}
function checkSfzhm(idcard) {
    if (isEmpty(idcard))return true;
    var area = {11:"\u5317\u4eac",12:"\u5929\u6d25",13:"\u6cb3\u5317",14:"\u5c71\u897f",15:"\u5185\u8499\u53e4",21:"\u8fbd\u5b81",22:"\u5409\u6797",23:"\u9ed1\u9f99\u6c5f",31:"\u4e0a\u6d77",32:"\u6c5f\u82cf",33:"\u6d59\u6c5f",34:"\u5b89\u5fbd",35:"\u798f\u5efa",36:"\u6c5f\u897f",37:"\u5c71\u4e1c",41:"\u6cb3\u5357",42:"\u6e56\u5317",43:"\u6e56\u5357",44:"\u5e7f\u4e1c",45:"\u5e7f\u897f",46:"\u6d77\u5357",50:"\u91cd\u5e86",51:"\u56db\u5ddd",52:"\u8d35\u5dde",53:"\u4e91\u5357",54:"\u897f\u85cf",61:"\u9655\u897f",62:"\u7518\u8083",63:"\u9752\u6d77",64:"\u5b81\u590f",65:"\u65b0\u7586",71:"\u53f0\u6e7e",81:"\u9999\u6e2f",82:"\u6fb3\u95e8",91:"\u56fd\u5916"}
    var idcard,Y,JYM;
    var S,M;
    var idcard_array = new Array();
    idcard_array = idcard.split("");
    if (area[parseInt(idcard.substr(0, 2))] == null) {
        return false;
    }
    switch (idcard.length) {
        case 15:
            if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 || ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 && (parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 )) {
                ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//\u6d4b\u8bd5\u51fa\u751f\u65e5\u671f\u7684\u5408\u6cd5\u6027
            } else {
                ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//\u6d4b\u8bd5\u51fa\u751f\u65e5\u671f\u7684\u5408\u6cd5\u6027
            }
            if (ereg.test(idcard)) {
                return 'yes';
            }
            else {
                return false;
            }
            break;
        case 18:
            if (parseInt(idcard.substr(6, 4)) % 4 == 0 || (parseInt(idcard.substr(6, 4)) % 100 == 0 && parseInt(idcard.substr(6, 4)) % 4 == 0 )) {
                ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//\u95f0\u5e74\u51fa\u751f\u65e5\u671f\u7684\u5408\u6cd5\u6027\u6b63\u5219\u8868\u8fbe\u5f0f
            } else {
                ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//\u5e73\u5e74\u51fa\u751f\u65e5\u671f\u7684\u5408\u6cd5\u6027\u6b63\u5219\u8868\u8fbe\u5f0f
            }
            if (ereg.test(idcard)) {
                S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
                        + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
                        + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
                        + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
                        + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
                        + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
                        + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
                        + parseInt(idcard_array[7]) * 1
                        + parseInt(idcard_array[8]) * 6
                        + parseInt(idcard_array[9]) * 3;
                Y = S % 11;
                M = "F";
                JYM = "10X98765432";
                M = JYM.substr(Y, 1);
                if (M == idcard_array[17]) {
                    return 'yes';
                }
                else {
                    return false;
                }
            }
            else {
                return false;
            }
            break;
        default:
            return false;
            break;
    }
}
function bindTips() {
    var FOCUS_COLOR = ""
    var BLUR_COLOR = "#999999"
    jQuery("input[tips]").each(function() {
        var item = jQuery(this)
        var tips = item.attr("tips")
        item.css({"line-height":"16px"})
        if (item.val() == "") {
            item.val(tips)
            item.css("color", BLUR_COLOR)
        } else {
            item.css("color", FOCUS_COLOR)
        }
        item.focus(function() {
            if (item.val() == tips) {
                item.val("")
            }
            item.css("color", FOCUS_COLOR)
        })
        item.blur(function() {
            if (item.val() == "") {
                item.val(tips)
                item.css("color", BLUR_COLOR)
            }
        })
    })
}
function cutText(area, maxLength) {
    var padText = "..."
    var step = padText.length
    jQuery(area).each(function() {
        var text = jQuery(this).text()
        if (text.length >= maxLength) {
            jQuery(this).text(text.substring(0, maxLength - step) + padText)
        }
    }
            )
}
function cutAllText() {
    var padText = "..."
    var step = padText.length
    jQuery("a[maxLength]").each(function() {
        var item = jQuery(this)
        var text = item.text()
        var maxLength = item.attr("maxLength")
        if (!item.attr("title")) {
            item.attr("title", text)
        }
        if (text.length >= maxLength) {
            jQuery(this).text(text.substring(0, maxLength - step) + padText)
        }
    }
            )
}
function breakOffText(area, maxLength) {
    var padText = "</br>"
    var step = padText.length
    jQuery(area).each(function() {
        var text = jQuery(this).text()
        if (text.length >= maxLength) {
            jQuery(this).text(text.substring(0, maxLength) + padText + text.substring(maxLength))
        }
    }
            )
}
function bindSave() {
    jQuery("#doSave").click(function() {
        if (!jQuery("#submitForm").valid()) {
            jQuery(".error:first").select();
            return false;
        }
        jQuery("#submitForm").submit();
        alert("保存成功");
    });
}
jQuery(document).ready(function() {
    next();
    cutAllText();
});